package com.cq.sphtech.net;

import android.util.Log;

import com.cq.sphtech.api.IpService;
import com.cq.sphtech.model.BaseJson;
import com.cq.sphtech.model.FlowInfo;


import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.cq.sphtech.net.RestCreator.getRxRestService;


public class IpInfoTask implements NetTask<String> {
    private static final String TAG = "IpInfoTask";

    private static IpInfoTask INSTANCE = null;

    private Disposable disposable;

    public IpInfoTask() {

    }

    public static IpInfoTask getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new IpInfoTask();
        }
        return INSTANCE;
    }

    @Override
    public Disposable execute(final String resource_id, final LoadTasksCallBack callBack) {
        IpService service = getRxRestService().create(IpService.class);
        service.getFlowInfo(resource_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseJson<FlowInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        callBack.onStart();
                        disposable =d;
                    }

                    @Override
                    public void onNext(BaseJson<FlowInfo> baseJson) {
                        if(baseJson.success){
                            callBack.onSuccess(baseJson.result);
                        }else {
                            Log.d(TAG,"实体没有解析到");
                            callBack.onFailed();
                        }
                    }
                    @Override
                    public void onError(Throwable t) {
                        Log.d(TAG,t.getMessage());
                        callBack.onFailed();
                    }
                    @Override
                    public void onComplete() {
                        callBack.onFinish();
                    }
                });
        return disposable;
    }
}
